package com.tpc.wdt.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tpc.wdt.WdtClient;
import com.tpc.wdt.utils.JDBCUtil;
import com.tpc.wdt.utils.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class JoinOutPush {
    @Value("${wdt.sid}")
    private String sid;

    @Value("${wdt.appkey}")
    private String appkey;

    @Value("${wdt.appsecret}")
    private String appsecret;

    @Value("${wdt.url}")
    private String url;

    @Value("${wdt.joinout.sql}")
    private String joinOutSql;

    public void joinOutPush(){
        WdtClient client = new WdtClient(sid, appkey, appsecret, url);

        Connection connection = JDBCUtil.getConnection();
        QueryRunner queryRunner = new QueryRunner();
        String isUpload = "";
        String uploadInfo = "";
        try {
            Map<String, Object> result = queryRunner.query(connection, joinOutSql, new MapHandler());
            String valueInfo = JSON.toJSONString(result);
            if(valueInfo.equals("null")) {
                return;
            }
            JSONObject jsonObject = (JSONObject) JSONObject.toJSON(JSONUtil.transToLowerObject(valueInfo));

            Map<String, Object> stockout_info = new HashMap<String, Object>();
            List<Map<String, Object>> detail_list = new ArrayList<Map<String, Object>>();

            Map<String, Object> detail_1 = new HashMap<String, Object>();
            detail_1.put("spec_no", jsonObject.getString("spec_no"));
            detail_1.put("num", jsonObject.getString("num"));
            detail_1.put("price", jsonObject.getString("price"));
            detail_1.put("batch_no", jsonObject.getString("batch_no"));
            detail_1.put("remark", jsonObject.getString("remark"));
            detail_list.add(detail_1);

            stockout_info.put("outer_no", jsonObject.getString("outer_no"));
            stockout_info.put("warehouse_no", jsonObject.getString("warehouse_no"));
            stockout_info.put("goods_total_amount", jsonObject.getString("goods_total_amount"));
            stockout_info.put("remark", jsonObject.getString("remark"));
            stockout_info.put("is_enable_negative", jsonObject.getString("is_enable_negative"));
            stockout_info.put("is_check", jsonObject.getString("is_check"));



            stockout_info.put("detail_list", detail_list);

            String stockout_info_json = JSON.toJSONString(stockout_info);

            Map<String, String> params = new HashMap<String, String>();
            params.put("stockout_info", stockout_info_json);
            try {
                log.info("其他出库请求信息：{}",JSONObject.toJSONString(params));
                String response = client.execute("stockout_order_push.php", params);
                log.info("其他出库商品返回信息：{}",response);
                JSONObject jsonResponse = JSONObject.parseObject(response);
                uploadInfo = jsonResponse.getString("message");
                if(jsonResponse.getString("stockout_no")!=null){
                    uploadInfo = uploadInfo + jsonResponse.getString("stockout_no");
                }
//                String stockoutNo = jsonResponse.getString("stockout_no");
                if(jsonResponse.getInteger("code")==0){
                    isUpload = "Y";
                }else{
                    isUpload = "N";
                }
                StringBuilder updateSql = new StringBuilder();
                if(jsonObject.getString("bs").equals("CT")) {
                    updateSql.append("update joinindt set IS_WDT_NEW = '" + isUpload + "',WDT_INFO = '" + uploadInfo + "' where 'CT-'||billno ||'-'||billsn = '" + jsonObject.getString("outer_no") + "'");
                }else if(jsonObject.getString("bs").equals("XSTR")) {
                    updateSql.append("update trgoodsdt set is_wdt_new = '" +isUpload + "',WDT_INFO = '" + uploadInfo + "' where 'XS-'||billno ||'-'||billsn = '" + jsonObject.getString("outer_no") +"'");
                }else if(jsonObject.getString("bs").equals("XSRET")) {
                    updateSql.append("update retgoodsdt set is_wdt_new = '" +isUpload + "',WDT_INFO = '" + uploadInfo + "' where 'XS-'||billno ||'-'||billsn = '" + jsonObject.getString("outer_no") +"'");
                } else if (jsonObject.getString("bs").equals("DB")) {
                    updateSql.append("update MOVEOUTDT set is_wdt_new = '" +isUpload + "',WDT_INFO = '" + uploadInfo + "' where 'DB-'||billno ||'-'||billsn = '" + jsonObject.getString("outer_no") +"'");
                } else if (jsonObject.getString("bs").equals("BS")) {
                    updateSql.append("update ALEXECDT set is_wdt_new = '" +isUpload + "',WDT_INFO = '" + uploadInfo + "' where 'BS-'||billno ||'-'||billsn = '" + jsonObject.getString("outer_no") +"'");
                } else if (jsonObject.getString("bs").equals("CL")) {
                    updateSql.append("update GoodsDestMt set is_wdt_new = '" +isUpload + "',WDT_INFO = '" + uploadInfo + "' where 'CL-'||billno = '" + jsonObject.getString("outer_no") +"'");
                }
                log.info("更新：{}",updateSql);
                queryRunner.update(connection,updateSql.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection);
        }
    }
}
